package com.android.delivery.activity.fragment

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.RecyclerView
import com.android.delivery.R
import com.android.delivery.activity.PrintlnActivity
import com.android.delivery.adapter.LPrintAdapter
import com.android.delivery.api.ApiService
import com.android.delivery.base.BaseListFragmet
import com.android.delivery.callback.DialogCallback
import com.android.delivery.callback.LzyResponse
import com.android.delivery.constant.Constants
import com.android.delivery.entity.Logistics
import com.android.delivery.entity.even.RefreshPrint
import com.android.delivery.util.ToastUtils
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.include_refresh_layout.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 揽件中心-已打印列表
 */
class LPrintedListFragment : BaseListFragmet<Logistics>() {

    private val jieDanAdapter: LPrintAdapter by lazy {
        LPrintAdapter()
    }


    companion object {
        fun newInstance(type: Int): LPrintedListFragment {
            val fragment = LPrintedListFragment()
            val bundle = Bundle()
            bundle.putInt(Constants.TYPE, type)
            fragment.arguments = bundle
            return fragment
        }
    }

    private var type: Int = 0

    override fun bindRecyclerView(recLv: RecyclerView?) {
        type = arguments!!.getInt(Constants.TYPE)

        recLv!!.adapter = jieDanAdapter
        jieDanAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (view.id == R.id.tv_1) {
                val item = jieDanAdapter.getItem(position)
                ApiService.printCloud(this, item!!.logisticsNo, object : DialogCallback<LzyResponse<Any>>() {
                    override fun onSuccess(response: Response<LzyResponse<Any>>?) {
                        if (response?.body()?.code == 60001) {
                            var intent = Intent(activity, PrintlnActivity::class.java)
                            startActivityForResult(intent, PrintlnActivity.REQUEST_CODE)
                        } else {
                            ToastUtils.showShort(activity, "打印成功")
                            onRefresh(refreshLayout)
                        }
                    }
                })
            }
        }

    }

    override fun loadData(page: Int, pageCount: Int) {
        ApiService.pick(this, page, type, object : DialogCallback<LzyResponse<Logistics>>() {
            override fun onSuccess(response: Response<LzyResponse<Logistics>>?) {
                onGetDataSuc(response!!.body().data.records)
            }
        })
    }

    override fun getData(): MutableList<Logistics> {
        return jieDanAdapter.data
    }

    override fun bindData(isRefresh: Boolean, list: MutableList<Logistics>?) {
        if (isRefresh) {
            jieDanAdapter.setNewData(list)
        } else {
            jieDanAdapter.addData(list!!)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun addrEven(item: RefreshPrint) {
        onRefresh(refreshLayout)
    }


    override fun isBindEventBus(): Boolean {
        return true
    }
}